//生成菜单
var menuItem = Vue.extend({
	name: 'menu-item',
	props: { item: {} },
	template: [
		'<li>',
		'<a v-if="item.type === 0" href="javascript:;">',
		'<i v-if="item.icon != null" :class="item.icon"></i>',
		'<span>{{item.name}}</span>',
		'<i class="fa fa-angle-left pull-right"></i>',
		'</a>',
		'<ul v-if="item.type === 0" class="treeview-menu">',
		'<menu-item :item="item" v-for="item in item.list"></menu-item>',
		'</ul>',
		'<a v-if="item.type === 1" :href="\'#\'+item.url"><i v-if="item.icon != null" :class="item.icon"></i><i v-else class="fa fa-circle-o"></i> {{item.name}}</a>',
		'</li>'
	].join('')
});

//iframe自适应
$(window).on('resize', function () {
	var $content = $('.content');
	$content.height($(this).height() - 120);
	$content.find('iframe').each(function () {
		$(this).height($content.height());
	});
}).resize();

//测试按键是否可以行
//$(window).on('keyup',function(){this.keyUp});

//注册菜单组件
Vue.component('menuItem', menuItem);

var vm = new Vue({
	el: '#rrapp',
	data: {
		user: {},
		menuList: {},
		main: "sys/main.html",
		password: '',
		newPassword: '',
		navTitle: "控制台"
	},
	methods: {

		cockpit: function () {
			var feature = "channelmode=yes, titlebar=no, toolbar=no, scrollbars=no," +
				"resizable=yes, status=no, copyhistory=no, location=no, menubar=no,width=" + (screen.availWidth) +
				"height=" + (screen.availHeight);
			window.open("../cockpit/index.html", feature);


		},
		getMenuList: function (event) {
			$.getJSON("sys/menu/user?_" + $.now(), function (r) {
				vm.menuList = r.menuList;
				window.permissions = r.permissions;
			});
		},
		getUser: function () {
			$.getJSON("sys/user/info?_" + $.now(), function (r) {
				vm.user = r.user;

				localStorage.setItem("userName", r.user.username);
				localStorage.setItem("trueName", r.user.truename);
				localStorage.setItem("department", r.user.department);
				localStorage.setItem("power", r.user.power);
			});
		},
		updatePassword: function () {
			layer.open({
				type: 1,
				skin: 'layui-layer-molv',
				title: "修改密码",
				area: ['550px', '270px'],
				shadeClose: false,
				content: jQuery("#passwordLayer"),
				btn: ['修改', '取消'],
				btn1: function (index) {
					var data = "password=" + vm.password + "&newPassword=" + vm.newPassword;
					$.ajax({
						type: "POST",
						url: "sys/user/password",
						data: data,
						success: function (result) {
							if (result.code == 0) {
								layer.close(index);
								layer.alert('修改成功', function (index) {
									location.reload();
								});
							} else {
								layer.alert(result.msg);
							}
						}
					});
				}
			});
		}
	},
	created: function () {
		console.log('create...');
		this.getMenuList();
		this.getUser();
	},
	updated: function () {
		//路由
		console.log('update....')
		var router = new Router();
		routerList(router, vm.menuList);
		router.start();
	}
});



function routerList(router, menuList) {
	for (var key in menuList) {
		var menu = menuList[key];
		if (menu.type == 0) {
			routerList(router, menu.list);
		} else if (menu.type == 1) {
			router.add('#' + menu.url, function () {
				var url = window.location.hash;
				console.log('url=' + url);
				//替换iframe的url
				vm.main = url.replace('#', '');

				//后台过期判断 2021-04-18
				console.log('catch the 11111!');
				let req = new XMLHttpRequest();
				req.open('GET', document.location, false);
				req.send(null);
				let headers = req.getAllResponseHeaders().toLowerCase();
				console.log(headers);
				// 由于返回的是用\r\n来进行分割的字符串，需要做转换
				let arr = headers.trim().split(/[\r\n]+/);
				let headerMap = {};
				arr.forEach(function (line) {
					let parts = line.split(': ')
					let header = parts.shift()
					let value = parts.join(': ')
					headerMap[header] = value
				})
				let statuscode = headerMap['statuscode'];
				console.log('statucode:= '+statuscode);

				//导航菜单展开
				$(".treeview-menu li").removeClass("active");
				$("a[href='" + url + "']").parents("li").addClass("active");
				
				vm.navTitle = $("a[href='" + url + "']").text();

				

			});
		}
	}
}
